package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreceiveList(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().lambda().eq(PurchaseEntity::getStatus, 0).or().eq(PurchaseEntity::getStatus, 1)
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void merge(MergeVo vo) {
        if (vo.getPurchaseId() == null) {
            //新建一个采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            vo.setPurchaseId(purchaseEntity.getId());
        }
        List<Long> items = vo.getItems();
        Long purchaseId = vo.getPurchaseId();
        List<PurchaseDetailEntity> collect = items.stream().map(s -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(s);
            purchaseDetailEntity.setPurchaseId(purchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDatailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void received(List<Long> ids) {
        //确定登入人 是采购单上的负责人

        //确认采购单是新建或者女已分配状态
        //修改采购单状态
        List<PurchaseEntity> collect = ids.stream().map(id -> this.getById(id))
                .filter(s -> {
                    if (s.getStatus() == 0 || s.getStatus() == 1) {
                        return true;
                    }
                    return false;
                }).map(s -> {
                    s.setStatus(2);
                    s.setUpdateTime(new Date());
                    return s;
                }).collect(Collectors.toList());
        this.updateBatchById(collect);

        //修改采购需求项状态
        List<Long> purchaseIds = collect.stream().map(s -> s.getId()).collect(Collectors.toList());
        purchaseDetailService.updataPurchaseDetail(purchaseIds);


    }
    @Autowired
    private WareSkuService wareSkuService;

    @Override
    @Transactional(rollbackFor = Exception.class )
    public void done(PurchaseDoneVo vo) {
        Boolean flag=true;
        Long id = vo.getId();
        //修改采购项
        List<PurchaseItemDoneVo> list = vo.getList();
        ArrayList<PurchaseDetailEntity> detailEntities = new ArrayList<>();
        for (PurchaseItemDoneVo purchaseItemDoneVo : list) {
            if (purchaseItemDoneVo.getStatus()==4) {
                flag=false;
            }else {
                //TODO 商品入库
                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(purchaseItemDoneVo.getItemId());
                wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());


            }
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            BeanUtils.copyProperties(purchaseItemDoneVo,purchaseDetailEntity);
            purchaseDetailEntity.setId(purchaseItemDoneVo.getItemId());
            detailEntities.add(purchaseDetailEntity);
        }
        purchaseDetailService.updateBatchById(detailEntities);
        //修改采购单
     if (flag){
         PurchaseEntity entity = new PurchaseEntity();
         entity.setId(id);
         entity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
         this.updateById(entity);
     }else {
         PurchaseEntity entity = new PurchaseEntity();
         entity.setId(id);
         entity.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode());
         this.updateById(entity);
     }

    }

}
